import Vue from 'vue'
import Vuex from 'vuex'
import dlqhttp from './common/http'
import dlqutil from './common/util'
import router from './router'

Vue.use(Vuex)

export default new Vuex.Store({
	state: {
		userInfo : {},
		menuList : [],
		systemName : '',
		URL : {
			upload : '/api/xagrid/upload',
		},
		systemList : [],

		allMenu : [],

		pageData : {},

		router : [],

	},

	getters : {
		userInfo : state => state.userInfo,
		menuList : state => state.menuList,
		systemName : state => state.systemName,
		URL : state => state.URL,
		center : state => state.center,
		city : state => state.city,
		systemList : state => state.systemList,
		pageData : state => state.pageData,
		router : state => state.router,
	},

	mutations: {
		setUserInfo (state, obj) {
		    state.userInfo = obj;
		},
		setSystemList (state, obj) {
	        state.systemList = obj;
	    },
		setMenuList (state, obj) {
	        state.menuList = obj;
	    },
	    setSystemName (state, obj) {
	        state.systemName = obj;
	    },
	    setAllMenuList (state, obj) {
	        state.allMenu = obj;
	    },
	    setMenu (state, menuIndex) {
	        state.menuList = state.allMenu[0].children[menuIndex].children;
	    },

	    setPageData (state, obj) {

	    	console.log('obj');
	    	console.log(obj);

            let col = [];

            obj.columns.forEach(item => {
            	if(item.show == '1'){
            		col.push(item);
            	}
            })

            col.unshift({
                type: 'selection',
                width: 60,
                align: 'center'
            });
	    	col.push({
                title: '操作',
                slot: 'action'
            });

            obj.columns = col;

	    	state.pageData = obj;
	    },

	    setRouter (state, obj) {
	    	state.router = obj;

	    	router.addRoutes(obj);
	    }
	},

	actions: {
		getUserInfo ({ commit, dispatch}, para) {
			// dlqhttp({
			// 	fun : 'userInfo',
			// 	//requestType : 'get',
			// 	cb : res => {
			// 		console.log(res);
			// 		commit('setUserInfo', res);
			// 	}
			// });
		},


		getRouter ({ commit, dispatch}, para) {
			console.log('getrouter');

			let arr = [
			 	{
			 		path: '/',
		            name: 'index',
		            component: () => import('./views/index.vue'),
		            children : [
		                {
		                    path: '/test',
		                    name: 'test',
		                    meta: {
		                        test : true,
		                        name : 'account',
		                    },
		                    component: () => import('./views/test/table.vue')
		                },
		            ]
			 	}
			];
			commit('setRouter', arr);
		},

		getMenuList ({ commit, dispatch}, para) {
			dlqhttp({
				fun : 'menuList',
				cb : res => {
					// console.log(res);

					const result = dlqutil.transData(res, 'id', 'pid', 'children');
					//console.log(result);

					let systemList = [];
					result[0].children.forEach((item, index) => {
						systemList.push({
							name : item.name,
							index,
						});
					});

					commit('setAllMenuList', result);
					commit('setSystemList', systemList);
					commit('setMenu', 0);
					
				}
			});
		},
	}
})
